/*******************************************************************************
TilesetKit.cpp

	Author:		LiuDingXi
	e-mail:		liudx@gmail.com
	Date time:	2009-08-06

	Copyright (C) 2009
	All rights reserved.
	
*******************************************************************************/
#include "StdAfx.h"
#include "TilesetKit.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

////////////////////////////////////////////////////////////////////////////////

CTilesetKit::CTilesetKit()
{
	m_nTotal = 0;
	m_pTiles = NULL;

	m_nWidth = 0;
	m_nHeight = 0;

	m_nSelect = -1;
	m_nCX = 0;
	m_nCY = 0;
	m_nCol = 0;
	m_nRow = 0;
}

CTilesetKit::~CTilesetKit()
{
	Free();
}

BOOL CTilesetKit::Load(CArchive& ar)
{
	TCHAR szFolder[MAX_PATH];
	 _tcscpy(szFolder, ar.GetFile()->GetFilePath());
	 LPTSTR pTail = PathFindFileName(szFolder);
	 if(pTail)
	 {
		 *pTail = 0;
	 }
	 m_szFolder = szFolder;

	CString s;
	while(ar.ReadString(s))
	{
		if(s.IsEmpty())
			continue;
		
		LPCTSTR p = s;
		MoveToBegin(p);
		
		#define IS_TAG(tag)	_tcsnicmp(p, tag, _tcslen(tag)) == 0
		
		if(IS_TAG(_T("tile_size")))
		{
			LPCTSTR ps = p + 9;
			m_nTx = ParseOf(ps, 16);
			m_nTy = ParseOf(ps, 16);
		}
		else if(IS_TAG(_T("image")))
		{
			if(!LoadImage(p + 5))
			{
				return FALSE;
			}
		}
		else if(IS_TAG(_T("flag")))
		{
			SetTileFlag(p + 4);
		}
	}	
	return TRUE;
}

BOOL CTilesetKit::Store(CArchive& ar)
{
	CString s;

	s.Format(_T("tile_size %d %d\r\n"), m_nTx, m_nTy);
	ar.WriteString(s);

	s.Format(_T("image %s\r\n"), m_szImage);
	ar.WriteString(s);

	for(int i=0; i<m_nTotal; i++)
	{
		if(m_pTiles[i].nFlags != 0)
		{
			s.Format(_T("\tflag %d %d\r\n"), i, m_pTiles[i].nFlags);
			ar.WriteString(s);
		}
	}

	return TRUE;
}

BOOL CTilesetKit::Free()
{
	delete[] m_pTiles;
	m_pTiles = NULL;

	return TRUE;
}

BOOL CTilesetKit::LoadImage(LPCTSTR p)
{
	m_szImage = ParseOf(p);

	CxImage xImage;

	CString szImageFile;
	szImageFile = m_szFolder;
	szImageFile += m_szImage;

	if(xImage.Load(szImageFile))
	{
		int w = xImage.GetWidth();
		int h = xImage.GetHeight();

		int tx = (w / m_nTx);
		int ty = (h / m_nTy);

		int nTiles = tx * ty;

		if(nTiles > 0)
		{
			m_nWidth = tx * m_nTx;
			m_nHeight = ty * m_nTy;

			m_nCol = tx;
			m_nRow = ty;

			TILE* pTiles = new TILE[nTiles];
			if(pTiles)
			{
				m_nTotal = nTiles;
				m_pTiles = pTiles;

				int x = 0;
				int y = 0;

				for(int i=0; i<nTiles; i++)
				{
					RECT rect;
					rect.left = x;
					rect.right = x + m_nTx;
					rect.top = y;
					rect.bottom = y + m_nTy;

					pTiles[i].nIdx = i;
					pTiles[i].nFlags = 0;

					xImage.Crop(rect, &(pTiles[i].xImage));

					x += m_nTx;
					if(x >= m_nWidth)
					{
						y += m_nTy;
						x = 0;
					}
				}
				return TRUE;
			}
		}
	}
	return FALSE;
}

void CTilesetKit::SetTileFlag(LPCTSTR p)
{
	int nIndex = ParseOf(p, -1);
	int nFlags = ParseOf(p, 0);

	if(nIndex >= 0 && nIndex < m_nTotal)
	{
		if(m_pTiles)
		{
			m_pTiles[nIndex].nFlags = nFlags;
		}
	}
}

CTilesetKit::TILE* CTilesetKit::GetTile(int nIndex)
{
	if(m_pTiles)
	{
		if(nIndex >= 0 && nIndex < m_nTotal)
		{
			return (m_pTiles + nIndex);
		}
	}
	return NULL;
}
